package com.kingyea.mobilepolice.apimanage.paraminfo.service.impl;

import com.kingyea.mobilepolice.apimanage.paraminfo.bean.ParamInfo;
import com.kingyea.mobilepolice.apimanage.paraminfo.dao.ParamInfoMapper;
import com.kingyea.mobilepolice.apimanage.paraminfo.service.ParamInfoService;
import com.kingyea.mobilepolice.apimanage.systemsync.Send2Queue;
import com.kingyea.mobilepolice.apimanage.utils.NumberUtils;
import com.kingyea.mobilepolice.apimanage.utils.StateCode;
import com.kingyea.mobilepolice.system.exception.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Mr.Lin
 * @version v1.0.0
 * @description ParamInfo 实现类
 * @date Created in 2018-03-11 11:20:05
 */
@Service
@Transactional
public class ParamInfoServiceImpl implements ParamInfoService {

    private static final Logger LOGGER = LoggerFactory.getLogger(ParamInfoServiceImpl.class);

    @Resource
    private ParamInfoMapper paramInfoMapper;

    /**
     * 通过ID查找对象
     *
     * @param id
     * @return
     */
    @Override
    public ParamInfo getById(Integer id) throws Exception {
        return this.paramInfoMapper.getById(id);
    }

    @Override
    public List<ParamInfo> getParamInfoByServiceId(Integer serviceId) {
        return paramInfoMapper.getByServiceId(serviceId);
    }

    /**
     * 列表
     *
     * @param
     * @return
     */
    @Override
    public List<ParamInfo> list(ParamInfo paramInfo) throws Exception {
        List<ParamInfo> list = paramInfoMapper.list(paramInfo);
        return list;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Send2Queue
    @Override
    public int deleteById(Integer id) throws Exception {
        paramInfoMapper.deleteById(id);
        return 1;
    }

    /**
     * 保存
     *
     * @param paramInfo
     * @return
     */
    @Send2Queue
    @Override
    public ParamInfo save(ParamInfo paramInfo) throws Exception {
        if (StringUtils.isEmpty(paramInfo.getName()) || StringUtils.isEmpty(paramInfo.getServiceId()) || NumberUtils.isNullOrLEZero(paramInfo.getServiceId())) {
            throw new BusinessException(StateCode.NO_ARGUMENT_ERROR);
        }
        // 检查数据重复
        ParamInfo isExistObj = paramInfoMapper.getByNameAndServiceId(paramInfo.getName(), paramInfo.getServiceId());
        if (isExistObj != null) {
            throw new BusinessException(StateCode.SERVER_DATA_DUPLICATE_ERROR);
        }
        paramInfoMapper.save(paramInfo);
        return paramInfo;
    }

    /**
     * 更新
     *
     * @param paramInfo
     * @return
     */
    @Send2Queue
    @Override
    public ParamInfo update(ParamInfo paramInfo) throws Exception {
        if (StringUtils.isEmpty(paramInfo.getName()) || StringUtils.isEmpty(paramInfo.getServiceId()) || NumberUtils.isNullOrLEZero(paramInfo.getServiceId()) || NumberUtils.isNullOrLEZero(paramInfo.getId())) {
            throw new BusinessException(StateCode.NO_ARGUMENT_ERROR);
        }
        // 检查除了自己外有没有其它记录用了 name 和 serviceId 组合主键
        ParamInfo isExistObj = paramInfoMapper.getByNameAndServiceIdAndExcludeID(paramInfo.getName(), paramInfo.getServiceId(), paramInfo.getId());
        if (isExistObj != null) {
            throw new BusinessException(StateCode.SERVER_DATA_DUPLICATE_ERROR);
        }
        paramInfoMapper.update(paramInfo);
        return paramInfo;
    }

    @Send2Queue
    @Override
    public void deletesByServiceId(Integer serviceId) {
        paramInfoMapper.deletesByServiceId(serviceId);
    }

    @Override
    public ParamInfo getByNameAndServiceId(String name, Integer serviceId) {
        return paramInfoMapper.getByNameAndServiceId(name, serviceId);
    }


}
